﻿/*

========================================================================

	                        Light Prepass Renderer

		                        by Floris Groen

                         E-mail : fmfgroen@yahoo.com
                     Blog : www.florisgroen.blogspot.com
		      

========================================================================

*/

using Microsoft.Xna.Framework.Graphics;

namespace LightPrePassRenderer.Engine
{
    public class ModelInstance
    {
        public ModelInstance(Model model)
        {
            this.model = model;
        }

        Model model;

        public void Draw(GraphicsDevice graphicsDevice)
        {
            //This will not work for a model with bones.
            //Normally I would create an entry in the render queue
            //per mesh part with its appropriate transform,
            //but for this demo decided to keep it simple.

            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (ModelMeshPart meshPart in mesh.MeshParts)
                {
                    graphicsDevice.VertexDeclaration = meshPart.VertexDeclaration;

                    graphicsDevice.Vertices[0].SetSource
                    (
                        mesh.VertexBuffer,
                        meshPart.StreamOffset,
                        meshPart.VertexStride
                    );

                    graphicsDevice.Indices = mesh.IndexBuffer;

                    graphicsDevice.DrawIndexedPrimitives
                    (
                        PrimitiveType.TriangleList,
                        meshPart.BaseVertex,
                        0,
                        meshPart.NumVertices,
                        meshPart.StartIndex,
                        meshPart.PrimitiveCount
                    );
                }
            }
        }
    }
}
